package com.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.entity.*;
import com.entity.vo.OperateLogExcelVO;
import com.entity.vo.ZhongbiaogonggaoExcelVO;
import com.service.ExcelService;
import com.service.OperateLogService;

import com.service.ZaixiantoubiaoService;
import com.service.ZhongbiaogonggaoService;
import com.utils.MPUtil;
import com.utils.PageUtils;
import com.utils.Query;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.el.ELException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
*  ExcelServiceImpl 接口实现
 * Copyright(c)2016-2022, 贵阳巴士快运旅游有限公司
 * 项目名称: 黔爽巴士
 * Author:suxu (zsx317127523@163.com)
 */
@Service
public class ExcelServiceImpl implements ExcelService {

    @Autowired
    private OperateLogService operateLogService;

    @Autowired
    private ZhongbiaogonggaoService zhongbiaogonggaoService;

    @Autowired
    private ZaixiantoubiaoService zaixiantoubiaoService;


    @Override
    public  List<OperateLogExcelVO> exportOperateLog(Map<String, Object> params){
        EntityWrapper<OperateLogEntity> ew = new EntityWrapper<>();
        OperateLogEntity operateLogEntity = new OperateLogEntity();
//        List<OperateLogEntity> list = operateLogService.selectList(
//                new EntityWrapper<OperateLogEntity>().eq("title", title)
//                .orderBy("create_time", false));
        PageUtils page = operateLogService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, operateLogEntity), params), params));


        List<OperateLogEntity> list = (List<OperateLogEntity>) page.getList();
        if(list.size() < 1){
            throw new ELException("没有可导出的数据！");
        }

        List<OperateLogExcelVO> voList = CollUtil.newArrayList();
        for (OperateLogEntity operateLogEntity1 : list ) {
            OperateLogExcelVO vo = new OperateLogExcelVO();
            BeanUtils.copyProperties(operateLogEntity1, vo);

            voList.add(vo);
        }
        return voList;
    }

    @Override
    public  List<ZhongbiaogonggaoExcelVO> exportZhongbiaoExcel(Map<String, Object> params){
        EntityWrapper<ZhongbiaogonggaoEntity> ew = new EntityWrapper<>();
        ZhongbiaogonggaoEntity zhongbiaogonggaoEntity = new ZhongbiaogonggaoEntity();
//        List<OperateLogEntity> list = operateLogService.selectList(
//                new EntityWrapper<OperateLogEntity>().eq("title", title)
//                .orderBy("create_time", false));
        PageUtils page = zhongbiaogonggaoService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, zhongbiaogonggaoEntity), params), params));


        List<ZhongbiaogonggaoEntity> list = (List<ZhongbiaogonggaoEntity>) page.getList();
        if(list.size() < 1){
            throw new ELException("没有可导出的数据！");
        }

        List<ZhongbiaogonggaoExcelVO> voList = CollUtil.newArrayList();
        for (ZhongbiaogonggaoEntity zhongbiaogonggaoEntity1 : list ) {
            ZhongbiaogonggaoExcelVO vo = new ZhongbiaogonggaoExcelVO();
            BeanUtils.copyProperties(zhongbiaogonggaoEntity1, vo);

           List<ZaixiantoubiaoEntity> zaixiantoubiaoEntityList =  zaixiantoubiaoService.selectList(
                    new EntityWrapper<ZaixiantoubiaoEntity>().eq("xiangmubianhao", vo.getXiangmubianhao()));

           StringBuilder cbgys = new StringBuilder();
           if(zaixiantoubiaoEntityList.size() > 0){
               for (ZaixiantoubiaoEntity zaixiantoubiaoEntity : zaixiantoubiaoEntityList) {
                   cbgys.append(zaixiantoubiaoEntity.getGongyingshangmingcheng()).append("-").append(zaixiantoubiaoEntity.getToubiaojiage()).append(System.lineSeparator());
               }
           }


            vo.setCbgys(String.valueOf(cbgys));
            voList.add(vo);
        }
        return voList;
    }


}

